package com.kuang.rw;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @program: juc
 * @description: 读写锁
 * @author: 陈恩涛
 * @create: 2021-02-24 11:33
 **/

/*
*  ReadWriteLock
*  读-读  可以共存
*  读-写  不能共存
*  写-写  不能共存
*
*  独占锁（写锁）
*  共享锁（读锁）
* */
public class ReadWriteLockDemo {
    public static void main(String[] args) {
        MyCacheLock myCache = new MyCacheLock();

        //写入
        for (int i = 0; i < 5; i++) {
            final int tmp = i;
            new Thread(() -> {
                myCache.put(""+tmp,""+tmp);
            },String.valueOf(i)).start();
        }

        //读取
        for (int i = 0; i < 5; i++) {
            final int tmp = i;
            new Thread(() -> {
                myCache.get(""+tmp);
            },String.valueOf(i)).start();
        }
    }
}

/*
*  自定义缓存
* */
class MyCache{

    private volatile Map<String,Object> map = new HashMap<>();

    //存，写
    public void put(String key,Object value){
        System.out.println(Thread.currentThread().getName()+"写入"+key);
        map.put(key,value);
        System.out.println(Thread.currentThread().getName()+"写入完毕！");
    }

    //取，读
    public Object get(String key) {
        System.out.println(Thread.currentThread().getName()+"读取"+key);
        Object o = map.get(key);
        System.out.println(Thread.currentThread().getName()+"读取完毕！");
        return o;
    }
}

// 加锁的缓存
class MyCacheLock{

    private volatile Map<String,Object> map = new HashMap<>();
    // 读写锁，更加细粒度的控制
    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    //存，写入的时候只希望同时只用一个线程写
    public void put(String key,Object value){
        readWriteLock.writeLock().lock();
        try {
            System.out.println(Thread.currentThread().getName()+"写入"+key);
            map.put(key,value);
            System.out.println(Thread.currentThread().getName()+"写入完毕！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }

    //取，读取的时候所有人都可以读

    public Object get(String key) {

        readWriteLock.readLock().lock();
        Object o = new Object();

        try {
            System.out.println(Thread.currentThread().getName()+"读取"+key);
            o = map.get(key);
            System.out.println(Thread.currentThread().getName()+"读取完毕！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.readLock().unlock();
        }
        return o;
    }
}


























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































